﻿using Microsoft.AspNetCore.Mvc;
using TeachlyQBank.DbServer;
using TeachlyQBank.Models;
using System;
using System.Collections.Generic;
using static System.Net.Mime.MediaTypeNames;

namespace TeachlyQBank.Controllers
{
    public class ExamPlanController : Controller
    {
        private readonly ExamServer _examServer;
        private readonly PaperServer _paperServer;
        private readonly UserServer _userServer;
        public ExamPlanController(ExamServer examServer, PaperServer paperServer, UserServer userServer)
        {
            _examServer = examServer;
            _paperServer = paperServer;
            _userServer = userServer;
        }

        // 跳转到添加考试页面
        public IActionResult Jump()
        {
            return View("AddExam");
        }

        public IActionResult AddExam()
        {
            List<Paper>papers = new List<Paper>();  
            papers=_paperServer.GetAllPapers();
            return View(papers);
        }

        // 添加考试信息
        [HttpPost]
        public IActionResult AddExamSumbit(Exam exam)
        {
            if (_examServer.AddExamServer(exam) > 0)
            {
                TempData["Message"] = "创建成功了";
                return RedirectToAction("ExamInfo");
            }
            else
            {
                TempData["Message"] = "哎呀出现了意料之外的结果呢，添加失败";
            }
            return RedirectToAction("ExamInfo");
        }

        // 查询所有考试信息
        public IActionResult ExamInfo()
        {
            List<Exam> resultList = _examServer.SltServer();
            return View("ExamInfo", resultList);
        }

        // 删除考试信息
        [HttpPost]
        public IActionResult DelSumbit(Exam exam)
        {
            if (_examServer.DelExamServer(exam) > 0)
            {
                TempData["message"] = "删除成功";
                return RedirectToAction("ExamInfo");
            }
            return RedirectToAction("ExamInfo");
        }

        // 更新考试信息
        [HttpPost]
        public IActionResult UpdSumbit(Exam exam)
        {
            if (_examServer.UpdServer(exam) > 0)
            {
                TempData["Message"] = "修改成功了！";
                TempData["AutoFillExamName"] = exam.exam_plan_name;
            }
            else
            {
                TempData["Message"] = "哎呀出现了意料之外的结果呢，修改失败";
            }
            return RedirectToAction("ExamInfo");
        }

        // 跳转到编辑考试信息页面
        public IActionResult UpdJump(Exam exam)
        {
            Exam exam1 = _examServer.SltUpdServer(exam);
            return View("EditExam", exam1);
        }

        // 根据考试信息查询考试信息
        [HttpPost]
        public IActionResult SltJump(Exam exam1)
        {
            List<Exam> resultList = _examServer.Slt2Server(exam1);
            return View("ExamInfo", resultList);
        }

        public IActionResult SelectPaper()
        {
            return View();
        }
     }
}

